package com.taoist_awei.blog.infrastructure.service;

import com.taoist_awei.blog.core.IDService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

@Service
public class DefaultIDService implements IDService {
    private Set<String> pool;
    private int i;

    @Autowired
    public DefaultIDService() {
        pool = new HashSet<>();
        Random random = new Random(System.currentTimeMillis());
        for (int i = 0; i < 10000000; ++i) {
            Long l = Math.abs(random.nextLong() / 100000000);
            pool.add(l.toString());
        }
        i = -1;
    }

    @Override
    public String getID() {
        synchronized (this) {
            i += 1;
            return (String) pool.toArray()[i];
        }
    }
}
